from functools import lru_cache
import os
import logging

from .base import BaseConfig
from ..common import helper

RUN_ENV = os.environ.get('RUN_ENV') or 'dev'

RUN_ENV = RUN_ENV.lower()

if RUN_ENV == 'prod':
    from .prod import ProdConfig as ConfigObj
elif RUN_ENV == 'local':
    from .local import LocalConfig as ConfigObj
else:
    from .dev import DevConfig as ConfigObj


#conf = ConfigObj()

def init_conf(ConfigObj: BaseConfig):
    #### 初始化配置
    print('---------- init -----------')
    print('RUN_ENV: %s' % RUN_ENV)
    conf: BaseConfig = ConfigObj    
    print('ORIGINS: %s' % conf.ORIGINS)
    print('ALLOWED_HOSTS: %s' % conf.ALLOWED_HOSTS)
    print('ALLOWED_URIS: %s' % conf.ALLOWED_URIS)
    print('SERVCIE_URLS: %s' % conf.SERVCIE_URLS)

    try:
        conf.DB_PASS = helper.decrypt_ecb_from_hex(conf.DB_PASS, conf.SECRET_KEY)

        conf.SQLALCHEMY_DATABASE_URL = "mysql+pymysql://%s:%s@%s/%s?charset=utf8mb4" % (
            conf.DB_USER,
            conf.DB_PASS,
            conf.DB_HOST,
            conf.DB_NAME
        )
      
        if RUN_ENV != 'prod':
            print('解密DB_PASS: %s' % conf.DB_PASS)
            print('SQLALCHEMY_DATABASE_URL: %s' % conf.SQLALCHEMY_DATABASE_URL)
            print(f'Redis: {conf.REDIS_HOST}:{conf.REDIS_PORT}')
    except Exception as err:
        print('解密DB_PASS错误, Error: %s' % err)
        

    return conf



 